/**
 * 角色role.js
 * 版本号： v1.0.0
 */
$(function(){
    var lastChecked = null;
    $("#role_dataList").datagrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        border: false,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/role/roleList.do',
        idField: 'id',
        striped:true,
        frozenColumns:[[
            {field:'id',checkbox:true}
        ]],
        columns: [
            [
                {field: 'name', title: '角色名称', width: 190, fixed: true},
                {field: 'dataScope', title: '数据权限', width: 250, fixed: true,formatter:function(val){
                    if(val){
                        return val.propertyName;
                    }
                }},
                {field: 'isSys', title: '是否系统数据', width: 110, fixed: true, formatter:function(val){
                    if(val == 1) return "<span>是</span>";
                    if(val == 2) return "<span >否</span>";
                }},
                {field: 'useable', title: '是否可用', width: 110, fixed: true, formatter:function(val){
                    if(val == 1) return "<span style='color: green; font-weight: bold'>启用</span>";
                    if(val == 2) return "<span style='color: red; font-weight: bold'>禁用</span>";
                }},
                {field: 'createDate', title: '创建时间', width: 160, fit:true, formatter: function (val) {
                        return formatDatebox(val,"yyyy-MM-dd HH:mm:ss", true);
                    }}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#role_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#role_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                lastChecked = null;
                $("#role_dataList").datagrid("selectRow", 0);
            }
        }
    });


    //定义可操作按钮
    $("#role_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            window.location.href = CONTEXT_PATH + "/role/toRoleOption.do";
                        }
                    });
                }
                //修改按钮
                if("editBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            role_edit();
                        }
                    });
                }
                //删除按钮
                if("delBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            role_del();
                        }
                    });
                }
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            role_findList();
                        }
                    });
                }
                //详情按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            role_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 删除角色
 */
function role_del() {
    var row = $("#role_dataList").datagrid("getSelected");
    var isSysAdmin = $("#isSysAdmin").val();
    var checked = $("#role_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择删除角色", timeout: 3000, showType: "slide" });
        return;
    }
    if (row.isSys == 1 && !(isSysAdmin == "true")) {
        $.messager.show({ title: '操作提示', msg: "该角色为系统角色，只有系统管理员能删除", timeout: 3000, showType: "slide" });
        return;
    }
    //判断角色是否是系统管理员
    if(row.id == "0"){
        $.messager.show({ title: '操作提示', msg: "系统管理员角色不能被删除！", timeout: 3000, showType: "slide" });
        return;
    }
    $.messager.confirm('删除提示', '您确定要删除 ' + row.name + ' 角色吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/role/del.do?id=" + row.id,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#role_dataList").datagrid("reload", {
                        name:$("#name_").val(),
                        dataScope:$("#dataScope_").combobox("getValue"),
                        isSys:$("#isSys_").combobox("getValue"),
                        useable:$("#useable_").combobox("getValue")
                    });
                }
            })
        }
    });
}

/**
 * 修改角色
 * @returns {boolean}
 */
function role_edit(){
    var row = $("#role_dataList").datagrid("getSelected");
    var isSysAdmin = $("#isSysAdmin").val();
    var checked = $("#role_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title:'操作提示', msg:"请选择修改角色", timeout:3000, showType:"slide" });
        return;
    }
    if (row.isSys == 1 && !(isSysAdmin == "true")) {
        $.messager.show({ title: '操作提示', msg: "该角色为系统角色，只有系统管理员能删除", timeout: 3000, showType: "slide" });
        return;
    }
    window.location.href = CONTEXT_PATH + "/role/toRoleOption.do?id=" + row.id;
}

/**
 * 刷新角色
 */
function role_view(iconCls){
    var row = $("#role_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的角色详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(juesxq.id, juesxq.url + row.id, iconCls, juesxq.closable, juesxq.title);
}

//查询角色
function role_findList(){
    $("#role_dataList").datagrid("reload",{
        name:$("#name_").val(),
        dataScope:$("#dataScope_").combobox("getValue"),
        isSys:$("#isSys_").combobox("getValue"),
        useable:$("#useable_").combobox("getValue")
    });
    //清楚选中项
    $("#role_dataList").datagrid("clearSelections");
}